package com.yfjin.ffmpeg54

import android.os.Bundle
import android.os.Environment
import android.util.Log
import android.view.LayoutInflater
import android.widget.SeekBar
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.yfjin.ffmpeg54.databinding.ActivityMain2Binding
import com.yfjin.ffmpeg54.player.IPlayerListener
import com.yfjin.ffmpeg54.player.MNPlayer
import com.yfjin.ffmpeg54.util.DisplayUtil


//音频都要解码重采样，因为要适配具体硬件
class MainActivityAudio : AppCompatActivity() {

    companion object {
        init {
            System.loadLibrary("audio-native-lib")
        }
    }


    private var mTotalTime: Int=0;
    private val mPlayer = MNPlayer()

    private lateinit var mBinding: ActivityMain2Binding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mBinding = DataBindingUtil.inflate(
            LayoutInflater.from(this),
            R.layout.activity_main2,
            null,
            false
        )
        setContentView(mBinding.getRoot())




        mPlayer.setPlayerListener(object : IPlayerListener {
            override fun onLoad(load: Boolean) {
            }

            override fun onCurrentTime(currentTime: Int, totalTime: Int) {

                mTotalTime=totalTime

                runOnUiThread {
                    mBinding.audioProgress.setProgress(currentTime * 100 / totalTime)
                    mBinding.auidoNowtime.setText(
                        DisplayUtil.secdsToDateFormat(
                            currentTime,
                            totalTime
                        )
                    )
                    mBinding.auidoEndtime.setText(
                        DisplayUtil.secdsToDateFormat(
                            totalTime,
                            totalTime
                        )
                    )
                }
            }

            override fun onError(code: Int, msg: String?) {
            }

            override fun onPause(pause: Boolean) {
            }

            override fun onDbValue(db: Int) {
            }

            override fun onComplete() {
            }

            override fun onNext(): String {
                return ""
            }
        })


        mPlayer.setMnOnPrepareListener {
            val threadName = Thread.currentThread().name
            runOnUiThread {
                ToastUtil.show("prepared ok:" + threadName);
                mPlayer.start()
            }
        }

        mBinding.audioProgress.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {

            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
            }

            override fun onStartTrackingTouch(p0: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
                val pos=seekBar!!.progress*mTotalTime/100
                Log.i("123123","seek:"+pos)
                mPlayer.seekTo(pos)
            }
        })


        mBinding.vPlay.setOnClickListener {
            mPlayer.setSource(Environment.getExternalStorageDirectory().path + "/123.mp3")
            mPlayer.prepared()
        }

//        mPlayer.setSource("http://ting6.yymp3.net:82/new27/zhaolei2/11.mp3")
//        mPlayer.setSource("http://mn.maliuedu.com/music/dengniguilai.mp3");




        mBinding.pause.setOnClickListener {
            if (mPlayer.pauseOrResume()){
                mBinding.pause.setText("Resume")
            }else{
                mBinding.pause.setText("Pause")
            }
        }

        mBinding.left.setOnClickListener {
            mPlayer.setMute(0)
        }

        mBinding.right.setOnClickListener {
            mPlayer.setMute(1)
        }

        mBinding.center.setOnClickListener {
            mPlayer.setMute(2)
        }


        mBinding.speed05.setOnClickListener {
            mPlayer.setSpeed(0.5f)
        }

        mBinding.speed1.setOnClickListener {
            mPlayer.setSpeed(1f)
        }

        mBinding.speed15.setOnClickListener {
            mPlayer.setSpeed(1.5f)
        }


        mBinding.pitch05.setOnClickListener {
            mPlayer.setPicth(0.5f)
        }

        mBinding.pitch1.setOnClickListener {
            mPlayer.setPicth(1f)
        }

        mBinding.pitch15.setOnClickListener {
            mPlayer.setPicth(1.5f)
        }


        mBinding.stop.setOnClickListener {
            mPlayer.stop()
        }
    }


}